home *** CD-ROM | disk | FTP | other *** search
- /*
- * hauif.c,v 1.1 1996/05/27 16:07:34 miksic Exp
- */
-
- /* HAUIF.C -- Example LONWORKS host application user interface code.
- * Copyright (c) 1993 by Echelon Corporation.
- * All Rights Reserved.
- */
-
- #include <ctype.h> /* For toupper() */
- #include <stdlib.h> /* For exit() */
- #include <string.h> /* For strlen(), strncpy() */
- #include <stdio.h> /* For printf() */
- #include "conio.h" /* for kbhit( ) and getch( ) */
- #include "ni_msg.h"
- #include "ni_mgmt.h"
- #include "hauif.h"
-
- #define BUF_SIZE 80
-
- /*************************** Function Prototypes ****************************/
-
-
- /* Command interpreter function handlers */
-
- void exit_func( void );
- void verbose( void );
- extern void traffic( void );
- extern void NV_table( void );
- extern void NV_update( void );
- extern void NV_poll( void );
- void null_cmd( void ) { }
-
- typedef struct {
- char letter; /* command letter */
- void ( * func )( void ); /* handler function */
- char * help_text;
- } command_struct;
-
- const static command_struct command_table[ ] = {
-
- { 'E', exit_func, "(E)xit this application and return to shell" },
- { 'N', NV_table, "(N)etwork Variable configuration table", },
- { 'P', NV_poll, "(P)oll input network variable" },
- { 'T', traffic, "Incoming network (T)raffic summary" },
- { 'U', NV_update, "(U)pdate network variable" },
- { 'V', verbose, "Control (V)erbose modes" },
- { '\r', null_cmd, "" },
- { '\0' }
- };
-
- /*
- ****************************************************************************
- * process_cmd(). Process a command from the keyboard
- ****************************************************************************
- */
-
- void process_cmd( void )
- {
- char ch;
- const command_struct * cmd_ptr;
-
- ch = getch( ); // read a character from keyboard
- ch = toupper( ch );
-
- for( cmd_ptr = command_table; cmd_ptr->letter; cmd_ptr++ ) {
- if( cmd_ptr->letter == ch ) {
- printf( "%s\n", cmd_ptr->help_text ); // echo command
- ( cmd_ptr->func ) ( ); // Dispatch command
- return;
- }
- }
- // List all the commands
-
- printf("\nDidn't recognize that command, valid commands are:\n" );
- for( cmd_ptr = command_table; cmd_ptr->letter != '\r'; cmd_ptr++ )
- printf( "%c -- %s.\n", cmd_ptr->letter, cmd_ptr->help_text );
-
- return;
- }
-
- /*
- ****************************************************************************
- * verbose mode command
- ****************************************************************************
- */
-
-
- void verbose( void ) {
- /* Control verbose and report modes. */
- extern boolean verbose_flag, report_flag;
-
- verbose_flag = get_choice(
- "Display network interface msgs (Y/N) ?",
- 'Y', 'N' );
- report_flag = get_choice(
- "Report incoming appl msgs (Y/N) ?",
- 'Y', 'N' );
- }
-
- /*
- ****************************************************************************
- *
- * get_integer - prompt and read an integer from the command line
- *
- ****************************************************************************
- */
-
- int get_integer( const char * prompt, int default_val ) {
- char input_buf[ BUF_SIZE ];
- int number;
-
- for( ;;) {
- printf( prompt );
- printf( " [%d] \t:", default_val );
- gets( input_buf ); // read a line from keyboard
- if( !input_buf[ 0 ] ) return default_val; // Empty line
- if( sscanf( input_buf, "%u", &number ) == 1 ) return number;
- printf( "Invalid integer\n" );
- }
- }
-
- /*
- ****************************************************************************
- *
- * get_choice - prompt and read a binary choice from the command line
- *
- ****************************************************************************
- */
-
- boolean get_choice( const char * prompt, char true_choice, char false_choice ) {
- // default choice is FALSE
- char ch;
-
- for( ;; ) {
- printf( prompt );
- ch = getch( );
- ch = toupper( ch ); // read a character from keyboard
- if( ch == '\r'|| ch == toupper( false_choice ) ) {
- printf( "%c\n", false_choice );
- return FALSE;
- }
- if( ch == toupper( true_choice ) ) {
- printf( "%c\n", true_choice );
- return TRUE;
- }
- printf( "Invalid choice\n" );
- }
- }
-
- /*
- ****************************************************************************
- *
- * sign-on message
- *
- ****************************************************************************
- */
-
- void sign_on( void ) {
-
- const command_struct * cmd_ptr;
-
- static const char sign_on_message[] = {
- "Welcome to the LONWORKS host application.\n\n"
- "This program acts as an application node\n"
- "Enter one of the following commands by typing the indicated letter:\n\n"
- };
-
- /* Display sign-on message. */
- printf( sign_on_message );
- for( cmd_ptr = command_table; cmd_ptr->letter != '\r'; cmd_ptr++ )
- printf( "%c -- %s.\n", cmd_ptr->letter, cmd_ptr->help_text );
- }
-
- /*
- ****************************************************************************
- *
- * error_exit - handle invocation errors
- *
- ****************************************************************************
- */
-
- void error_exit( void ) {
-
- static const char flag_message[] = {
- "\n\n"
- "To invoke the LONWORKS Host Application Demonstration Program, enter:"
- "\n\n"
- " ha [-Ddevice] [-V]\n\n"
- " -D selects the network device name, the default is 'LON1'.\n"
- " -V invokes verbose mode.\n"
- };
-
- printf( flag_message );
- exit( 1 );
- }
-
-